﻿// <copyright file="AlertConfigurationElementCollection.cs" company="http://www.dymetis.com">
//
// Copyright (c) 2011 Eddie Z. Lin.
//
// This source is subject to the Microsoft Public License.
// See http://www.opensource.org/licenses/ms-pl.
// All other rights reserved.
// 
// </copyright>

namespace Dymetis.TeamAlert.Configuration
{
    using System.Configuration;

    /// <summary>
    /// A collection of AlertConfigurationElement instances.
    /// </summary>
    [ConfigurationCollection(typeof(AlertConfigurationElement), CollectionType = ConfigurationElementCollectionType.BasicMapAlternate, AddItemName = AlertConfigurationElementCollection.AlertConfigurationElementPropertyName)]
    public partial class AlertConfigurationElementCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// The XML name of the individual <see cref="AlertConfigurationElement"/> instances in this collection.
        /// </summary>
        internal const string AlertConfigurationElementPropertyName = "alert";

        /// <summary>
        /// Gets the type of the <see cref="ConfigurationElementCollection"/>.
        /// </summary>
        /// <returns>The <see cref="ConfigurationElementCollectionType"/> of this collection.</returns>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMapAlternate;
            }
        }

        /// <summary>
        /// Gets the name used to identify this collection of elements
        /// </summary>
        protected override string ElementName
        {
            get
            {
                return AlertConfigurationElementCollection.AlertConfigurationElementPropertyName;
            }
        }

        /// <summary>
        /// Gets the <see cref="AlertConfigurationElement"/> at the specified index.
        /// </summary>
        /// <param name="index">The index of the <see cref="AlertConfigurationElement"/> to retrieve.</param>
        public AlertConfigurationElement this[int index]
        {
            get
            {
                return (AlertConfigurationElement)this.BaseGet(index);
            }
        }

        /// <summary>
        /// Gets the <see cref="AlertConfigurationElement"/> with the specified key.
        /// </summary>
        /// <param name="name">The key of the <see cref="AlertConfigurationElement"/> to retrieve.</param>
        public AlertConfigurationElement this[object name]
        {
            get
            {
                return (AlertConfigurationElement)this.BaseGet(name);
            }
        }

        /// <summary>
        /// Adds the specified <see cref="AlertConfigurationElement"/> to the <see cref="ConfigurationElementCollection"/>.
        /// </summary>
        /// <param name="alert">The <see cref="AlertConfigurationElement"/> to add.</param>
        public void Add(AlertConfigurationElement alert)
        {
            this.BaseAdd(alert);
        }

        /// <summary>
        /// Removes the specified <see cref="AlertConfigurationElement"/> from the <see cref="ConfigurationElementCollection"/>.
        /// </summary>
        /// <param name="alert">The <see cref="AlertConfigurationElement"/> to remove.</param>
        public void Remove(AlertConfigurationElement alert)
        {
            this.BaseRemove(this.GetElementKey(alert));
        }

        /// <summary>
        /// Gets the <see cref="AlertConfigurationElement"/> at the specified index.
        /// </summary>
        /// <param name="index">The index of the <see cref="AlertConfigurationElement"/> to retrieve.</param>
        /// <returns>
        /// The <see cref="AlertConfigurationElement"/> at the specified index.
        /// </returns>
        public AlertConfigurationElement GetItemAt(int index)
        {
            return (AlertConfigurationElement)this.BaseGet(index);
        }

        /// <summary>
        /// Gets the <see cref="AlertConfigurationElement"/> with the specified key.
        /// </summary>
        /// <param name="name">The key of the <see cref="AlertConfigurationElement"/> to retrieve.</param>
        /// <returns>
        /// The <see cref="AlertConfigurationElement"/> with the specified key.
        /// </returns>
        public AlertConfigurationElement GetItemByKey(string name)
        {
            return (AlertConfigurationElement)this.BaseGet((object)name);
        }

        /// <summary>
        /// Gets a value indicating whether the element is read-only.
        /// </summary>
        /// <returns>
        /// A value indicating whether the element is read-only.
        /// </returns>
        public override bool IsReadOnly()
        {
            return false;
        }

        /// <summary>
        /// Indicates whether the specified <see cref="ConfigurationElement"/> exists in the <see cref="ConfigurationElementCollection"/>.
        /// </summary>
        /// <param name="elementName">The name of the element to verify.</param>
        /// <returns>
        /// <see langword="true"/> if the element exists in the collection; otherwise, <see langword="false"/>.
        /// </returns>
        protected override bool IsElementName(string elementName)
        {
            return elementName == AlertConfigurationElementCollection.AlertConfigurationElementPropertyName;
        }

        /// <summary>
        /// Gets the element key for the specified configuration element.
        /// </summary>
        /// <param name="element">The <see cref="ConfigurationElement"/> to return the key for.</param>
        /// <returns>
        /// An <see cref="object"/> that acts as the key for the specified <see cref="ConfigurationElement"/>.
        /// </returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((AlertConfigurationElement)element).Name;
        }

        /// <summary>
        /// Creates a new <see cref="AlertConfigurationElement"/>.
        /// </summary>
        /// <returns>
        /// A new <see cref="AlertConfigurationElement"/>.
        /// </returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new AlertConfigurationElement();
        }
    }
}
